<title>「NOI2015」程序自动分析</title><link href="http://hzoi.com/tomorrow.css" rel="stylesheet"><div class="ui existing segment"><pre><code><span class="pl-cp">#include</span><span class="pl-cpf">&lt;iostream&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span><span class="pl-cpf">&lt;map&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span><span class="pl-cpf">&lt;vector&gt;</span><span class="pl-cp"></span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-n">std</span><span class="pl-p">;</span>
<span class="pl-kt">int</span> <span class="pl-n">t</span><span class="pl-p">,</span><span class="pl-n">n</span><span class="pl-p">,</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">,</span><span class="pl-n">e</span><span class="pl-p">,</span><span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-mi">200005</span><span class="pl-p">],</span><span class="pl-n">flag</span><span class="pl-p">,</span><span class="pl-n">cnt</span><span class="pl-p">;</span>
<span class="pl-n">vector</span><span class="pl-o">&lt;</span><span class="pl-n">pair</span><span class="pl-o">&lt;</span><span class="pl-kt">int</span><span class="pl-p">,</span><span class="pl-kt">int</span><span class="pl-o">&gt;</span> <span class="pl-o">&gt;</span><span class="pl-n">vec</span><span class="pl-p">;</span>
<span class="pl-n">map</span><span class="pl-o">&lt;</span><span class="pl-kt">int</span><span class="pl-p">,</span><span class="pl-kt">int</span><span class="pl-o">&gt;</span><span class="pl-n">Map</span><span class="pl-p">;</span>
<span class="pl-kt">void</span> <span class="pl-nf">init</span><span class="pl-p">(){</span>
    <span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-mi">2</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-o">++</span><span class="pl-n">i</span><span class="pl-p">)</span><span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">i</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">fd</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">){</span>
    <span class="pl-k">return</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span><span class="pl-o">==</span><span class="pl-n">x</span><span class="pl-o">?</span><span class="pl-nl">x</span><span class="pl-p">:(</span><span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">fd</span><span class="pl-p">(</span><span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]));</span>
<span class="pl-p">}</span>
<span class="pl-kt">bool</span> <span class="pl-nf">in_one</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-kt">int</span> <span class="pl-n">y</span><span class="pl-p">){</span>
    <span class="pl-k">return</span> <span class="pl-n">fd</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">)</span><span class="pl-o">==</span><span class="pl-n">fd</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">U</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-kt">int</span> <span class="pl-n">y</span><span class="pl-p">){</span>
    <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">fd</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">)]</span><span class="pl-o">=</span><span class="pl-n">fd</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">main</span><span class="pl-p">(){</span>
    <span class="pl-n">ios</span><span class="pl-o">::</span><span class="pl-n">sync_with_stdio</span><span class="pl-p">(</span><span class="pl-mi">0</span><span class="pl-p">);</span>
    <span class="pl-n">cin</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">t</span><span class="pl-p">;</span>
    <span class="pl-k">while</span><span class="pl-p">(</span><span class="pl-n">t</span><span class="pl-o">--</span><span class="pl-p">){</span>
        <span class="pl-n">Map</span><span class="pl-p">.</span><span class="pl-n">clear</span><span class="pl-p">();</span>
        <span class="pl-n">cnt</span><span class="pl-o">=</span><span class="pl-mi">0</span><span class="pl-p">;</span>
        <span class="pl-n">cin</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">n</span><span class="pl-p">;</span>
        <span class="pl-n">vec</span><span class="pl-p">.</span><span class="pl-n">clear</span><span class="pl-p">();</span>
        <span class="pl-n">init</span><span class="pl-p">();</span>
        <span class="pl-n">flag</span><span class="pl-o">=</span><span class="pl-mi">0</span><span class="pl-p">;</span>
        <span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-o">++</span><span class="pl-n">i</span><span class="pl-p">){</span>
            <span class="pl-n">cin</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">x</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">y</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">e</span><span class="pl-p">;</span>
            <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">Map</span><span class="pl-p">.</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">)</span><span class="pl-o">!=</span><span class="pl-n">Map</span><span class="pl-p">.</span><span class="pl-n">end</span><span class="pl-p">())</span><span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-n">Map</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">];</span>
            <span class="pl-k">else</span> <span class="pl-p">{</span><span class="pl-n">Map</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span><span class="pl-o">=++</span><span class="pl-n">cnt</span><span class="pl-p">;</span><span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-n">cnt</span><span class="pl-p">;}</span>
            <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">Map</span><span class="pl-p">.</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">)</span><span class="pl-o">!=</span><span class="pl-n">Map</span><span class="pl-p">.</span><span class="pl-n">end</span><span class="pl-p">())</span><span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">Map</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">];</span>
            <span class="pl-k">else</span> <span class="pl-p">{</span><span class="pl-n">Map</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">]</span><span class="pl-o">=++</span><span class="pl-n">cnt</span><span class="pl-p">;</span><span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">cnt</span><span class="pl-p">;}</span>
            <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">e</span><span class="pl-p">){</span>
                <span class="pl-n">U</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">);</span>
            <span class="pl-p">}</span><span class="pl-k">else</span><span class="pl-p">{</span>
                <span class="pl-n">vec</span><span class="pl-p">.</span><span class="pl-n">push_back</span><span class="pl-p">(</span><span class="pl-n">make_pair</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">));</span>
            <span class="pl-p">}</span>
        <span class="pl-p">}</span>
        <span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-n">vector</span><span class="pl-o">&lt;</span><span class="pl-n">pair</span><span class="pl-o">&lt;</span><span class="pl-kt">int</span><span class="pl-p">,</span><span class="pl-kt">int</span><span class="pl-o">&gt;</span> <span class="pl-o">&gt;::</span><span class="pl-n">iterator</span> <span class="pl-n">it</span><span class="pl-o">=</span><span class="pl-n">vec</span><span class="pl-p">.</span><span class="pl-n">begin</span><span class="pl-p">();</span><span class="pl-n">it</span><span class="pl-o">!=</span><span class="pl-n">vec</span><span class="pl-p">.</span><span class="pl-n">end</span><span class="pl-p">();</span><span class="pl-o">++</span><span class="pl-n">it</span><span class="pl-p">){</span>
            <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">in_one</span><span class="pl-p">(</span><span class="pl-n">it</span><span class="pl-o">-&gt;</span><span class="pl-n">first</span><span class="pl-p">,</span><span class="pl-n">it</span><span class="pl-o">-&gt;</span><span class="pl-n">second</span><span class="pl-p">)){</span>
                <span class="pl-n">cout</span><span class="pl-o">&lt;&lt;</span><span class="pl-s">&quot;NO&quot;</span><span class="pl-o">&lt;&lt;</span><span class="pl-n">endl</span><span class="pl-p">;</span>
                <span class="pl-n">flag</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span>
                <span class="pl-k">break</span><span class="pl-p">;</span>
            <span class="pl-p">}</span>
        <span class="pl-p">}</span>
        <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">flag</span><span class="pl-p">)</span><span class="pl-n">cout</span><span class="pl-o">&lt;&lt;</span><span class="pl-s">&quot;YES&quot;</span><span class="pl-o">&lt;&lt;</span><span class="pl-n">endl</span><span class="pl-p">;</span>
    <span class="pl-p">}</span>
    <span class="pl-k">return</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
</cpde></pre></div>